package etc.com.market;

import java.util.ArrayList;

import etc.com.sql.DBAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class InfomationAct extends Activity implements OnItemClickListener {
	/** Called when the activity is first created. */
	private ListView lv;
	private static final int REQUEST_CODE = 10;
	ArrayList<String> arrtext = new ArrayList<String>();
	ArrayList<String> arrId = new ArrayList<String>();
	String str;
	Cursor cursor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.information);

		TextView view = (TextView) findViewById(R.id.btnNext);
		view.setText("お知らせ一覧");

		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String messageReceive = extras.getString("message");

		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();

			TextView v = (TextView) findViewById(R.id.storename);
			v.setText(str);

		}

		// ImageView ImageView = (ImageView) findViewById(R.id.banner);
		// ImageView.setImageResource(R.drawable.header2);

		lv = (ListView) findViewById(R.id.listview);
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(this);
		lv.setAdapter(new ImageAdapter(this));

		DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
		cursor = dbAdapter.getListInfo();
		int i = 1;
		System.out.println("CORSOR INFO COUNT: " + cursor.getCount());
		while(cursor.moveToNext()){
			String title = cursor.getString(1) == null ? String.valueOf(i) + "." :  String.valueOf(i) + "." +  cursor.getString(1);
			arrtext.add(title);
			arrId.add(cursor.getString(0));
			i++;
		}
		
		// arrtext.add("2. Ex-Wiggle given $60,000 and hung out to dry");
		// arrtext.add("3. Roddick injury hands Hewitt win");
		// arrtext.add("4. Canadian bolter can win the Open");
		// arrtext.add("5. Hogg, MacGill cash in at BPL ");
		// arrtext.add("6. Stars down Strikers, reach semis  ");

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				onClick(view,
						("お知らせ一覧;" + str + ";" + arrId.get(position)), position);
			}
		});

	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}
	// function use for footer button ==============================
	public void Previous(View view) {
		//onClickPre(view, ("storename"));
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void Home(View view) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", str);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, "店舗詳細;" + str);
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, ("店舗詳細;" + str));
	}

	public void Info(View view) {

	}
	
	public void Contact(View view) {
		onClickContact(view, str);
	}

	// ===================================================================
	
	public void onClickPre(View view, String message) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClick(View view, String message, int position) {
		Intent i = new Intent(this, InfomationDetailAct.class);
		i.putExtra("message", message);
		i.putExtra("position", position);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	 public void onClickSearch(View view, String message) {
			Intent i = new Intent(this, SearchOursProducts.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);

		}

	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("returnKey1")) {
				Toast.makeText(this, data.getExtras().getString("returnKey1"),
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return arrtext.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(mContext);

			View row = inflater.inflate(R.layout.rowinfo, parent, false);

			TextView label = (TextView) row.findViewById(R.id.rowndata);

			label.setText(arrtext.get(position));

			return row;
		}
	}
}